package com.qf.zsq.controller;

import com.qf.entity.GoodsEntity;
import com.qf.entity.ResponseResult;
import com.qf.finalstr.FruitsFinal;
import com.qf.zsq.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
public class ShopCarController {


    @Autowired
    IGoodsService goodsService;

    //添加商品到购物车
    @RequestMapping("addShopCar")
    public ResponseResult addShopCar(String gid, HttpSession session) {

        ResponseResult rr = new ResponseResult();
        //添加到购物车
        Map<String, GoodsEntity> shopCar = (Map<String, GoodsEntity>) session.getAttribute("shopCar");

        if(null == shopCar){//说明第一次往购物车里面添加东西
            shopCar =new HashMap<>();//没有购物车 new 一个
        }
        String thiscount="";//保存的就是当前商品的总价格

        if(null == shopCar.get(gid)){//如果当前商品是第一次添加
            GoodsEntity goodsEntity =goodsService.queryGoodsDetail(gid);

            shopCar.put(gid,goodsEntity);

            thiscount+=goodsEntity.getGoodsPrice();
        }else{ //如果存在 直接对数量+1
            GoodsEntity goodsEntity = shopCar.get(gid);
            goodsEntity.setGoodsNumber(goodsEntity.getGoodsNumber()+1);

            thiscount += goodsEntity.getGoodsPrice()*goodsEntity.getGoodsNumber();

        }
        session.setAttribute("shopCar",shopCar);

        rr.setResult(FruitsFinal.SUCCESS_RESULT);
        rr.setResultMsg(getNumberAndPrice(shopCar)+","+thiscount);
        session.setAttribute("msg",rr);//保证页面跳转购物车不变
        session.setAttribute("allMoney",getNumberAndPrice(shopCar).split(",")[1]);
        return rr;
    }

    private String getNumberAndPrice(Map<String, GoodsEntity> shopCar){

        if(null == shopCar){
            return "0,0.00";
        }

        Iterator<GoodsEntity> iterator = shopCar.values().iterator();

        GoodsEntity ge;
        Integer numberCount = 0; //商品数量
        Float priceCount = 0F; //商品总价
        while(iterator.hasNext()){
            ge = iterator.next();
            numberCount += ge.getGoodsNumber();

            priceCount += ge.getGoodsNumber() * ge.getGoodsPrice()*ge.getGoodsDiscount();
        }

        return numberCount+","+priceCount;
    }

    //从购物车移除商品
    @RequestMapping("subShopCar")
    public ResponseResult subShopCar(String gid,HttpSession session) {
        ResponseResult rr = new ResponseResult();

        Map<String, GoodsEntity> shopCar = (Map<String, GoodsEntity>) session.getAttribute("shopCar");

        GoodsEntity ge = shopCar.get(gid);//从购物车获取该商品
        if (null == ge) {
            rr.setResult(FruitsFinal.NAME_NULL_RESULT);//非法操作
        }

        String thiscount = "";
        if (ge.getGoodsNumber() > 1) {
            ge.setGoodsNumber(ge.getGoodsNumber() - 1);
            thiscount += ge.getGoodsNumber() * ge.getGoodsPrice();
            rr.setResult("9000"); //成功 -1
        } else { //数量=1 把商品从购物车移除
            shopCar.remove(gid);
            rr.setResult("9001");//操作成功 移除
        }
        String count = getNumberAndPrice(shopCar);//获取购物车的总数量和总价格

        rr.setResultMsg(count + "," + thiscount);

        //将购物车回填
        session.setAttribute("shopCar", shopCar);

        return rr;
    }

    @RequestMapping("getMsg")
    public ResponseResult getMsg(HttpServletRequest request){
        ResponseResult rrmsg = (ResponseResult) request.getSession().getAttribute("msg");
        return rrmsg;
    }

}
